package org.springblade.modules.repair.vo.defert;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.repair.entity.MaintainPlanTemplate;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zcc
 * @version 1.0
 * @date 2022/11/21 14:38
 */
@Data
public class PlanAndSignDateVo {


	@ApiModelProperty("考勤时间")
	private Date signDate;

	@ApiModelProperty("0:休息日1:工作日,2:未排考勤")
	private Integer holiday = 2;


	@ApiModelProperty("工单类型半月，年度")
	private String planTypeName;


	@ApiModelProperty("工单类型半月，年度此字段用于前端处理其它逻辑处理")
	private String planTypeNames;

	@ApiModelProperty("工单id")
	private String planId;

	@ApiModelProperty("0:表示非当前计划1:表示初始计划 2:表示当前计划")
	private Integer historyPlan = 0;

	@ApiModelProperty("0:不可选 1:可选")
	private Integer isSelect = 0;

	private Integer planDate;


	public void setSignDate(Date signDate) {
		this.signDate = signDate;
		this.planDate = Convert.toInt(DateUtil.format(signDate, "yyyyMMdd"));
	}

	public void setPlanTypeName(String planTypeName) {
		if (Func.isNotEmpty(planTypeName) && planTypeName.contains("%")) {
			List<String> strings = Func.toStrList(planTypeName);
			List<MaintainPlanTemplate> list = new ArrayList<>(10);
			for (String str : strings) {
				String[] split = str.split("%");
				MaintainPlanTemplate maintainPlanTemplate = new MaintainPlanTemplate();
				maintainPlanTemplate.setPlanId(Convert.toLong(split[0]));
				maintainPlanTemplate.setTemplateName(split[2]);
				maintainPlanTemplate.setSort(Convert.toInt(split[3], 0));
				list.add(maintainPlanTemplate);
			}
			//处理单天多选任务的情况
			List<Long> planIds = list.stream().map(MaintainPlanTemplate::getPlanId).distinct().collect(Collectors.toList());
			if (planIds.size() > 1) {
				Map<Long, List<MaintainPlanTemplate>> longListMap = list.stream().collect(Collectors.groupingBy(MaintainPlanTemplate::getPlanId));
				List<String> planTypeNames = new ArrayList<>(2);
				for (Long planId : planIds) {
					List<String> collect = longListMap.get(planId).stream().sorted(Comparator.comparing(MaintainPlanTemplate::getSort)).map(MaintainPlanTemplate::getTemplateName).collect(Collectors.toList());
					String join = String.join(",", collect);
					planTypeNames.add(join);
				}
				this.planTypeName = String.join("%", planTypeNames);
			} else {
				List<String> collect = list.stream().sorted(Comparator.comparing(MaintainPlanTemplate::getSort)).map(MaintainPlanTemplate::getTemplateName).collect(Collectors.toList());
				this.planTypeName = String.join(",", collect);
			}
		} else {
			this.planTypeName = planTypeName;
		}
	}


}
